﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using Icoders.MarketTracker.Core.MasterData;
using Icoders.MarketTracker.Core.Repository.MasterDataRepositories.UserRepositories;
using Icoders.MarketTracker.Core.Validation;
using Icoders.MarketTracker.Data.EF;
using Icoders.MarketTracker.Data.Utils;

namespace Icoders.MarketTracker.Data.Repository.MasterDataRepositories.UserRepositories
{
    public class UserGroupRepository : RepositoryBase, IUserGroupRepository
    {
        private AppDataContext _ctx;
        public UserGroupRepository(AppDataContext ctx)
       {
           _ctx = ctx;
       }
        public ValidationResultInfo Validate(UserGroup objToValidate)
        {
            ValidationResultInfo vri = objToValidate.BasicValidation();
            bool hasDuplicateName = GetAll().Where(v => v.Id != objToValidate.Id)
                .Any(p => p.Name == objToValidate.Name);
            if (hasDuplicateName)
                vri.Results.Add(new ValidationResult("Duplicate Name Found"));
            return vri;
        }

        public int Save(UserGroup entity)
        {
            ValidationResultInfo vri = Validate(entity);
            if (!vri.IsValid)
            {
                throw new DomainValidationException(vri, "Group Details provided not Valid");
            }
            DateTime date = TimeUtil.GetDateTime();
            TblUserGroup tbl = _ctx.TblUserGroup.FirstOrDefault(s => s.Id == entity.Id);
            if (tbl == null)
            {
                tbl = new TblUserGroup();
                tbl.CreatedOn = date;
                tbl.StatusId = (int)MasterEntityStatus.New;
                _ctx.TblUserGroup.AddObject(tbl);

              
            }
            tbl.UpdatedOn = date;
            tbl.Name = entity.Name;
          

            _ctx.SaveChanges();
            return tbl.Id;
        }

        public UserGroup GetById(int id)
        {
            var tbl = _ctx.TblUserGroup.FirstOrDefault(s => s.Id == id);
            if (tbl == null) return null;
            return Map(tbl);
        }

        private UserGroup Map(TblUserGroup tbl)
        {
            return new UserGroup
            {
                Id = tbl.Id,
                Name = tbl.Name,
                UpdatedOn = tbl.UpdatedOn,
                CreatedOn = tbl.CreatedOn,
                Status = (MasterEntityStatus)tbl.StatusId
            };
        }

        public List<UserGroup> GetAll()
        {
            return _ctx.TblUserGroup.ToList().Select(n => Map(n)).ToList();
        }

        public void Delete(int id)
        {
            throw new NotImplementedException();
        }

        protected override string _cacheKey
        {
            get { throw new NotImplementedException(); }
        }

        protected override string _cacheListKey
        {
            get { throw new NotImplementedException(); }
        }
    }
}
